#include "menu.h"
#include "main.h"
#include "key_menu.h"
#include "oled.h"
#include "font.h"
#include "speed.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h> // 用于提供 PRIu8

uint8_t KeyNum;
static menu_state_t current_menu_state;

/**
  * @brief 屏幕初始化函数
  */
void menu_init(void){
    current_menu_state = MENU_STATE_MAIN;
}

  /**
  * @brief 菜单任务函数
  * @param  无
  * @retval 
  */
void menu_task(void){
    uint8_t menu_result;
    
    while(1){
        switch(current_menu_state){
            case MENU_STATE_MAIN:
                menu_result = menu1();
                if(menu_result == 1)
                    current_menu_state = MENU_STATE_SPEED;
                break;
                
            case MENU_STATE_SPEED:
                menu_result = menu2_speed();
                if(menu_result == 1)
                    current_menu_state = MENU_STATE_MAIN;
                else if(menu_result == 2)
                    current_menu_state = MENU_STATE_SPEED_MAX;
                else if(menu_result == 3)
                    current_menu_state = MENU_STATE_SPEED_MIN;
                break;
                
            case MENU_STATE_SPEED_MAX:
                menu_result = menu2_speed_max();
                if(menu_result == 3)
                    current_menu_state = MENU_STATE_SPEED;
                break;
                
            case MENU_STATE_SPEED_MIN:
                
                break;
            
        }
        HAL_Delay(20);
    }
}

/**
  * @brief 一级主菜单显示函数
  * @param  无
  * @retval flag,flag到第几行
  */
uint32_t menu1(void){
        
        
	int flag = 1;
	char buffer[32]; // 足够大的缓冲区，用来格式化字符串
    uint32_t last_update = 0;
    
    //静态数据
    OLED_NewFrame();
    OLED_PrintString(10, 0, "智能点滴系统", &font16x16, OLED_COLOR_NORMAL);
    OLED_PrintString(16, 16, "滴速:", &font15x15, OLED_COLOR_NORMAL);
    OLED_PrintString(16, 32, "输液情况:", &font15x15, OLED_COLOR_NORMAL);
    OLED_PrintString(46, 48, "设置", &font15x15, OLED_COLOR_NORMAL);   
    OLED_ShowFrame();// 初始渲染
    
    while(1){
        
        if (HAL_GetTick() - last_update > 500) {
            
            //更新滴速显示
            sprintf(buffer, "%.2f滴/min", get_drop_speed());
            OLED_PrintString(60, 16, buffer, &font15x15, OLED_COLOR_NORMAL);
          
            //检测堵塞
            if (HAL_GetTick() - get_drop_last_time() >= 6000)
            {
                OLED_PrintString(91, 32, "堵塞" , &font15x15, OLED_COLOR_NORMAL);
                set_drop_speed(0);
                HAL_GPIO_WritePin(GPIOE,GPIO_PIN_5,GPIO_PIN_SET);
                HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
            }
            else if(get_drop_speed() >= get_drop_speed_max()|| get_drop_speed() <= get_drop_speed_min())
            {
                OLED_PrintString(91, 32, "异常" , &font15x15, OLED_COLOR_NORMAL);
                HAL_GPIO_WritePin(GPIOE,GPIO_PIN_5,GPIO_PIN_SET);
                HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
            }
            else {
                OLED_PrintString(91, 32, "通畅" , &font15x15, OLED_COLOR_NORMAL);
                HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);
                HAL_GPIO_WritePin(GPIOE,GPIO_PIN_5,GPIO_PIN_RESET);//关闭报警
            }
            
            // 箭头显示
            OLED_PrintString(0, 16, flag == 1 ? "→" : "  ", &font15x15, OLED_COLOR_NORMAL);
            OLED_PrintString(0, 32, flag == 2 ? "→" : "  ", &font15x15, OLED_COLOR_NORMAL);
            OLED_PrintString(0, 48, flag == 3 ? "→" : "  ", &font15x15, OLED_COLOR_NORMAL);
            OLED_ShowFrame();

            last_update = HAL_GetTick();// 重置时间
        }
	
	
	
		KeyNum = get_key_num();
		if(KeyNum == 1)// 上一项
		{
			flag --;
			if (flag == 0){flag = 3;}
		}
		if(KeyNum == 2)// 下一项
		{
			flag ++;
			if (flag == 4){flag = 1;}
		}
		if(KeyNum == 3)// 确认
		{
			return flag;
		}
        	
            HAL_Delay(50);
	}
}
/**
 * @brief 二级滴速菜单显示函数
 * @param 无
 * @note 
 * @retval flagspeed,flagspeed到第几行
 */
uint32_t menu2_speed(void){
        
    char buffer_max[32];    
	int flagspeed = 1;

    
    //静态数据
    OLED_NewFrame();
    OLED_PrintString(16, 16, "Back", &font15x15, OLED_COLOR_NORMAL);
    OLED_PrintString(16, 32, "上限", &font15x15, OLED_COLOR_NORMAL);
    
    sprintf(buffer_max, "%"PRIu8"滴/min", get_drop_speed_max());
    OLED_PrintString(60, 32, buffer_max, &font15x15, OLED_COLOR_NORMAL);
    
    OLED_PrintString(16, 48, "下限", &font15x15, OLED_COLOR_NORMAL);   
    OLED_ShowFrame();// 初始渲染
	
	
	while(1){
		KeyNum = get_key_num();
		if(KeyNum == 1)// 上一项
		{
			flagspeed --;
			if (flagspeed == 0){flagspeed = 3;}
		}
		if(KeyNum == 2)// 下一项
		{
			flagspeed ++;
			if (flagspeed == 4){flagspeed = 1;}
		}
		if(KeyNum == 3)// 确认
		{
			OLED_NewFrame();
			return flagspeed;
		}
        
//        OLED_PrintString(0, 16, " ", &font15x15, OLED_COLOR_NORMAL); // 清除第 16 行的箭头
//        OLED_PrintString(0, 32, " ", &font15x15, OLED_COLOR_NORMAL); // 清除第 32 行的箭头
//        OLED_PrintString(0, 48, " ", &font15x15, OLED_COLOR_NORMAL); // 清除第 48 行的箭头		
        
            OLED_PrintString(0, 16, flagspeed == 1 ? "→" : "  ", &font15x15, OLED_COLOR_NORMAL);
            OLED_PrintString(0, 32, flagspeed == 2 ? "→" : "  ", &font15x15, OLED_COLOR_NORMAL);
            OLED_PrintString(0, 48, flagspeed == 3 ? "→" : "  ", &font15x15, OLED_COLOR_NORMAL);
            OLED_ShowFrame();
	}
}
/**
  * @brief  三级修改修改滴速报警上限函数
  * @param  无
  * @retval 
  */
uint32_t menu2_speed_max(void){
    
		char buffer_max[32]; // 足够大的缓冲区，用来格式化字符串

        //静态数据
        OLED_NewFrame();
        OLED_PrintString(16, 16, "Back", &font15x15, OLED_COLOR_NORMAL);
        OLED_PrintString(16, 32, "上限", &font15x15, OLED_COLOR_NORMAL);
        
        sprintf(buffer_max, "%"PRIu8"滴/min", get_drop_speed_max());
        OLED_PrintString(60, 32, buffer_max, &font15x15, OLED_COLOR_REVERSED);
        
        OLED_PrintString(16, 48, "下限", &font15x15, OLED_COLOR_NORMAL);   
        OLED_ShowFrame();// 初始渲染

		while(1){
		KeyNum = get_key_num();
		if(KeyNum == 1)// 上一项
		{
			set_drop_speed_max(get_drop_speed_max() - 10);// 每次减10
			if (get_drop_speed_max() <= 20){set_drop_speed_max(20);}// 下限最小为20
            sprintf(buffer_max, "%"PRIu8"滴/min", get_drop_speed_max());
            OLED_PrintString(60, 32, buffer_max, &font15x15, OLED_COLOR_REVERSED);
            OLED_ShowFrame();
		}
		if(KeyNum == 2)// 下一项
		{
			set_drop_speed_max(get_drop_speed_max() + 10);// 每次加10
			if (get_drop_speed_max() >= 120){set_drop_speed_max(120);}// 上限最大为120
            sprintf(buffer_max, "%"PRIu8"滴/min", get_drop_speed_max());
            OLED_PrintString(60, 32, buffer_max, &font15x15, OLED_COLOR_REVERSED);
            OLED_ShowFrame();
		}
		if(KeyNum == 3)// 确认
		{
			return KeyNum;
		}
		
	}
}		
	
/**
  * @brief  修改滴速报警下限函数
  * @param  无
  * @retval 
  */	



